﻿#region using
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using AwesomeTanks.Sources.GameElements.TankElements;
#endregion

namespace AwesomeTanks.Sources.PlayerInputs
{
    /// <summary>
    /// Keyboard Controls for the Debug of AwesomeTanks
    /// </summary>
    public class KeyboardControlsState : IControlsState
    {
        #region Default Controls

        private const Keys UP_DEFAULT_BUTTON = Keys.Up;
        private const Keys DOWN_DEFAULT_BUTTON = Keys.Down;
        private const Keys LEFT_DEFAULT_BUTTON = Keys.Left;
        private const Keys RIGHT_DEFAULT_BUTTON = Keys.Right;
        //private const Keys DEPLOY_ITEM_DEFAULT_BUTTON = Keys.;
        //private const MouseState SHOT_DEFAULT_BUTTON 
        //private const Keys SPECIAL_SHOT_DEFAULT_BUTTON = Buttons.LeftTrigger;
        private const Keys PAUSE_DEFAULT_BUTTON = Keys.P;

        private Tank tank;

        #endregion

        #region Fields
        
        public TankAction PreviousTankAction { get; set; }

        #endregion

        #region Constructors


        /// <summary>
        /// Initializes a new instance of the <see cref="KeyboardControlsState"/> class.
        /// </summary>
        /// <param name="tank">The tank of the player. Used to determinate the angle of the aiming, between the player and the mouse pointer.</param>
        public KeyboardControlsState(Tank tank)
        {
            this.tank = tank;
        }
        #endregion 

        #region Methods
        /// <summary>
        /// Gets the tank direction.
        /// </summary>
        /// <returns></returns>
        public Vector2 GetTankDirection()
        {
            KeyboardState keyState = Keyboard.GetState();
            Vector2 direction = Vector2.Zero;

            if (keyState.IsKeyDown(UP_DEFAULT_BUTTON))
            {
                direction.Y -= 1;
            }

            if (keyState.IsKeyDown(DOWN_DEFAULT_BUTTON))
            {
                direction.Y += 1;
            }

            if (keyState.IsKeyDown(RIGHT_DEFAULT_BUTTON))
            {
                direction.X += 1;
            }

            if (keyState.IsKeyDown(LEFT_DEFAULT_BUTTON))
            {
                direction.X -= 1;
            }

            return direction;
        }

        /// <summary>
        /// Gets the tank aiming.
        /// </summary>
        /// <returns></returns>
        public Vector2 GetTankAiming()
        {
            MouseState mouseState = Mouse.GetState();
            Vector2 aiming = new Vector2(mouseState.X, mouseState.Y) - tank.Position;

            if (aiming != Vector2.Zero)
            {
                aiming = Vector2.Normalize(aiming);
            }

            return new Vector2(aiming.X, - aiming.Y);
        }

        /// <summary>
        /// Gets the tank action.
        /// </summary>
        /// <returns></returns>
        public TankAction GetTankAction()
        {
            TankAction action = TankAction.NONE;
            MouseState mouseState = Mouse.GetState();

            if (mouseState.RightButton == ButtonState.Pressed && PreviousTankAction != TankAction.SPECIAL_SHOT)
            {
                action = TankAction.SPECIAL_SHOT;
            }
            else if (mouseState.MiddleButton == ButtonState.Pressed && PreviousTankAction != TankAction.DEPLOY_ITEM)
            {
                action = TankAction.DEPLOY_ITEM;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed && PreviousTankAction != TankAction.SHOT)
            {
                action = TankAction.SHOT;
            }

            PreviousTankAction = action;
            return action;
        }


        /// <summary>
        /// Determines whether the keyboard is connected.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool IsConnected()
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
